/*******************************************************************
 *
 * Copyright (C), 2021-2022, Letstrong, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-14
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "icu.h"
#include "intc.h"
#include "string.h"
#include "stdbool.h"
#include "hal_hall.h"
#include "hal_gpio.h"
#include "user_config.h"

/*----------------------------macro file---------------------------*/

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/
static hal_hall_evt_cb_t hall_evt_cb = NULL;

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

static void hall_pin_int_hdl(uint8_t pin_num)
{
	if(hall_evt_cb) {
		hall_evt_cb(pin_num, true);
	}
}


void hal_hall_hw_init(hal_hall_evt_cb_t cb)
{
#if(HALL_PWR_EN)
	hal_gpio_output_cfg(HALL_PWR_PIN_NUM, PULL_UP);
	hal_gpio_write(HALL_PWR_PIN_NUM, 1);
#endif
	
	hal_gpio_input_cfg(HALL1_PIN_NUM, PULL_UP);
	hal_gpio_int_enable(HALL1_PIN_NUM, FALLING_EDGE, hall_pin_int_hdl);
	hal_gpio_input_cfg(HALL2_PIN_NUM, PULL_UP);
	hal_gpio_int_enable(HALL2_PIN_NUM, FALLING_EDGE, hall_pin_int_hdl);
#if(HALL_NUM >= 3)
    hal_gpio_input_cfg(HALL3_PIN_NUM, PULL_UP);
	hal_gpio_int_enable(HALL3_PIN_NUM, FALLING_EDGE, hall_pin_int_hdl);
#endif  //#if(HALL_NUM >= 3)

	hall_evt_cb = cb;
}


void hal_hall_hw_deinit(void)
{
	hal_gpio_int_disable(HALL1_PIN_NUM);
	hal_gpio_int_disable(HALL2_PIN_NUM);
#if(HALL_NUM >= 3)
    hal_gpio_int_disable(HALL3_PIN_NUM);
#endif
	
	hall_evt_cb = NULL;
	
#if(HALL_PWR_EN)
	hal_gpio_output_cfg(HALL_PWR_PIN_NUM, PULL_DOWN);
	hal_gpio_write(HALL_PWR_PIN_NUM, 0);
	hal_gpio_output_cfg(HALL1_PIN_NUM, PULL_DOWN);
	hal_gpio_write(HALL1_PIN_NUM, 0);
	hal_gpio_output_cfg(HALL2_PIN_NUM, PULL_DOWN);
	hal_gpio_write(HALL2_PIN_NUM, 0);
#if(HALL_NUM >= 3)
    hal_gpio_output_cfg(HALL3_PIN_NUM, PULL_DOWN);
	hal_gpio_write(HALL3_PIN_NUM, 0);
#endif  //#if(HALL_NUM >= 3)
#else
	//If hall no power control, hall output pin level is not confirmed, so we need to put it to high res.
	hal_gpio_hires_cfg(HALL1_PIN_NUM, PULL_NONE);
	hal_gpio_hires_cfg(HALL2_PIN_NUM, PULL_NONE);
#endif
}

